<template>
  <div>
    <el-dialog :visible="isShowRole" title="分配角色" @close="btnCancel">
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="role in RoleList" :key="role.id" :label="role.id">{{ role.name }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-row>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserId } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    isShowRole: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      RoleList: [],
      checkList: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取所有员工角色列表
    async getRoleList() {
      const { rows } = await getRoleList()
      this.RoleList = rows
    },
    // 获取员工已有的角色
    async getCheckedRole(id) {
      const { roleIds } = await getUserId(id)
      this.checkList = roleIds
    },
    async btnOK() {
      // 发送请求
      await assignRoles({ id: this.userId, roleIds: this.checkList })
      // 提醒用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:isShowRole', false)
    },
    btnCancel() {
      this.$emit('update:isShowRole', false)
    }
  }
}
</script>

<style>

</style>
